// import { routerRedux } from 'dva/router';
import { message } from 'antd';
import { getCompanyInfo, getAllCompanyInfo, getCompareData } from '@/services/evaluate';

export default {
  namespace: 'comparison',

  state: {
    defaultCompany: {
      name: '',
      code: '',
    },
    cmp: {
      name: '',
      code: '',
      fullName: '',
      market: '',
    },
    type: '利润比对',
    companies: [],
    allNumbers: [],
  },

  effects: {
    *getCompanyInfo({ payload }, { call, put }) {
      try {
        const cmp = yield call(getCompanyInfo, payload);
        if (+cmp.code !== 0) {
          // message.error(res.message);
          return;
        }
        yield put({
          type: 'setCmp',
          payload: {
            defaultCompany: {
              name: cmp.data.stock_name,
              code: cmp.data.stock_code,
            },
            cmp: {
              name: cmp.data.stock_name,
              code: cmp.data.stock_code,
              fullName: cmp.data.company_full_name,
              market: cmp.data.market,
            },
          },
        });
      } catch (e) {
        console.log(e);
      }
    },
    *getInitList({ payload }, { call, put }) {
      try {
        const cmp = yield call(getCompanyInfo, payload);
        if (+cmp.code !== 0) {
          message.error(res.message);
          return;
        }
        yield put({
          type: 'setCmp',
          payload: {
            defaultCompany: {
              name: cmp.data.stock_name,
              code: cmp.data.stock_code,
            },
            cmp: {
              name: cmp.data.stock_name,
              code: cmp.data.stock_code,
              fullName: cmp.data.company_full_name,
              market: cmp.data.market,
            },
          },
        });
        const res = yield call(getCompareData, { company_code: cmp.data.stock_code });

        if (+res.code === 0) {
          yield put({
            type: 'setData',
            payload: {
              allNumbers: res.data,
            },
          });
          // yield put({
          //   type: 'setNumbers',
          //   payload: {
          //     type: payload.type
          //     // numbers: res.data.numbers,
          //   },
          // });
        } else {
          message.error(res.message);
        }
      } catch (e) {
        message.error('获取数据失败，请稍后再试');
      }
    },
    *getCompareList({ payload }, { call, put }) {
      try {
        const res = yield call(getCompareData, { ...payload });
        if (+res.code === 0) {
          yield put({
            type: 'setData',
            payload: {
              allNumbers: res.data,
            },
          });
        } else {
          message.error(res.message);
        }
      } catch (e) {
        message.error('获取数据失败，请稍后再试');
      }
    },
    /**
     * 获取所有对比公司名称
     */
    *getAllCompanies({ payload }, { call, put }) {
      try {
        const res = yield call(getAllCompanyInfo, payload);
        if (+res.code === 0) {
          yield put({
            type: 'setAllCmps',
            payload: {
              companies: res.data.data_list,
            },
          });
        }
      } catch (e) {
        message.error('获取数据失败，请稍后再试');
      }
    },
  },
  reducers: {
    setData(state, { payload }) {
      const newState = {
        ...state,
        ...payload,
      };
      return newState;
    },
    // setNumbers(state, { payload }) {
    //   let {type, allNumbers} = payload;
    //   let numbers = allNumbers[type];
    //   const newState = {
    //     ...state,
    //     ...payload,
    //   };
    //   return newState;
    // },
    setAllCmps(
      state,
      {
        payload: { companies },
      }
    ) {
      return {
        ...state,
        companies,
      };
    },
    setCmp(
      state,
      {
        payload: { cmp },
      }
    ) {
      return {
        ...state,
        cmp,
      };
    },
  },
};
